package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.daoman.Floor;
import com.example.daoman.Repair;
import com.example.daoman.User;
import com.example.mapper.RepairMapper;
import com.example.mapper.UserMapper;
import com.example.service.RepairService;
import com.example.utils.GetSession;
import com.example.utils.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements RepairService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RepairMapper repairMapper;

    //    用户识别认证
    @Override
    public Message UserOne() {
        String account = GetSession.get().getAccount();  //学号 账号
        Map<String, Object> map = new HashMap<>();
        map.put("account", account);
        User user = userMapper.getAllList(map).get(0);
        return Message.out(200, "", user);
    }

    //    用户提交报修申请
    @Override
    public Message InsertRepair(List<Repair> repairList) {
        if (repairList.size()==0){
            return Message.out(200, "", false);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("list", repairList);
        boolean b = repairMapper.addRepair(map);
        return Message.out(200, "", b);
    }

    //    根据管理员的楼层查询自己楼层的报修情况
    @Override
    public Message getMyFloorRepair(List<Floor> floorList) {
//        获取楼层id
        Map<String, Object> map = new HashMap<>();
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (Floor f : floorList) {
            arrayList.add(f.getId());
        }
        map.put("list", arrayList);
        List<Repair> myRepairList = repairMapper.getMyRepairList(map);
        return Message.out(200, "成功", myRepairList);
    }

    //    报修删除操作根据id
    @Override
    public Message removeMyFloorRepair(Repair repair) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", repair.getId());
        map.put("txt", repair.getTxt());
        map.put("process", repair.getProcess());
        map.put("tid",GetSession.get().getId());
        boolean b = repairMapper.removeAndUpdate(map);
        return Message.out(200, "删除", b);
    }

    //    管理员受理该申请订单

    @Override
    public Message updateMyRepair(Repair repair) {
        Map<String, Object> map = new HashMap<>();
        map.put("process", repair.getProcess());
        map.put("id", repair.getId());
        map.put("tid", GetSession.get().getId());
        boolean b = repairMapper.updateMyRepair(map);
        return Message.out(200,"成功",b);
    }

//    用户查询自己的申请记录信息

    @Override
    public Message getUserApply() {
        try {
            int id = GetSession.get().getId();
            List<Repair> myApply = repairMapper.getMyApply(id);
            return Message.out(200,"查询",myApply);
        }catch (Exception e){
            return null;
        }
    }
}
